<?php
namespace Home\Controller;
use Admin\Logic\CacheLogic;
use Think\Controller;

class IndexController extends Controller {

    //网站配置
    public $all_config=array();

    //网站配置
    public $all_setting=array();

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->all_config=(new CacheLogic())->get_all_config();
        //下面加载所有的网站配置
        $this->all_setting=(new CacheLogic())->get_all_setting();
        //关闭了站点后直接访问后台
        if($this->all_setting['site_status']!=1){
            $this->redirect('/admin/index/index');
        }
        //下面判断是不是手机端访问
        if(isMobile()){
            $this->redirect('/mobile/index/index');
        }
        //下面加载网站的菜单
        $menu = M('daohang')->where(array('is_delete'=>0,'type'=>'pc','display'=>1))->order('ordid asc')->field('id,name,target,pid,url')->select();
        $menu = createGradeMenu($menu,'pid',0);
        $this->all_setting['slide_pc_pic']=json_decode($this->all_setting['slide_pc_pic'],true);
        $this->all_setting['slide_wap_pic']=json_decode($this->all_setting['slide_wap_pic'],true);
        $this->assign('setting',$this->all_setting);
        $this->assign('customer_apply_city',$this->all_config['customer_apply_city']);
        $this->assign('menu',$menu);
    }

    /**
     * 进入首页
     */
    public function index(){
        echo 'hello world';exit();
        //下面设置seo
        $seo_title='首页'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_title'];
        $seo_keys='首页'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_keys'];
        $seo_desc='首页'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_desc'];
        $this->assign('seo_array',array('seo_title'=>$seo_title, 'seo_keys'=>$seo_keys, 'seo_desc'=>$seo_desc));
        //下面获取  公司动态  团队介绍  金融须知 这三类文章各8片
        $news1=M('article')->where(array('cate_id'=>1,'status'=>1,'is_delete'=>0,'can_index'=>2))->limit(8)->order('id desc')->select();
        foreach($news1 as $k=>$v){
            $news1[$k]['introduce']=mb_substr(strip_tags(html_entity_decode($v['content'])),0,80,'utf-8');
            unset($news1[$k]['content']);
        }
        $news5=M('article')->where(array('cate_id'=>5,'status'=>1,'is_delete'=>0,'can_index'=>2))->limit(8)->order('id desc')->select();
        foreach($news5 as $k=>$v){
            $news5[$k]['introduce']=mb_substr(strip_tags(html_entity_decode($v['content'])),0,80,'utf-8');
            unset($news5[$k]['content']);
        }
        $news2=M('article')->where(array('cate_id'=>2,'status'=>1,'is_delete'=>0,'can_index'=>2))->limit(8)->order('id desc')->select();
        foreach($news2 as $k=>$v){
            $news2[$k]['introduce']=mb_substr(strip_tags(html_entity_decode($v['content'])),0,80,'utf-8');
            unset($news2[$k]['content']);
        }
        $productList1=M('product')->where(array('is_delete'=>0,'can_index'=>2))->limit(12)->order('ordid asc')->select();
        $this->assign('productList1',$productList1);
        $this->assign('news1',$news1);
        $this->assign('news5',$news5);
        $this->assign('news2',$news2);
        $this->display();
    }

    /**
     * 进入文章列表页
     */
    public function list1(){
        $get=I('get.');
        $cate_id=$get['id'];
        $cate_name=M('article_cate')->where(array('id'=>$cate_id))->find();
        $seo_title=$cate_name['seo_title']?$cate_name['seo_title']:$cate_name['name'].'-'.$this->all_setting['site_name'].$this->all_config['seo_default_title'];
        $seo_keys=$cate_name['seo_keys']?$cate_name['seo_keys']:$cate_name['name'].'-'.$this->all_setting['site_name'].$this->all_config['seo_default_keys'];
        $seo_desc=$cate_name['seo_desc']?$cate_name['seo_desc']:$cate_name['name'].'-'.$this->all_setting['site_name'].$this->all_config['seo_default_desc'];
        $this->assign('seo_array',array('seo_title'=>$seo_title, 'seo_keys'=>$seo_keys, 'seo_desc'=>$seo_desc));
        //下面设置面包屑
        $this->assign('mianbaoxie',array(
            array('name'=>'首页','url'=>'/'),
            array('name'=>$cate_name['name'],'url'=>U('Home/index/list1',array('id'=>$cate_id)))
        ));
        $p = $get['p'] ? $get['p'] : 1 ;
        $order='id desc';
        $perpage=5;
        //下面是构建条件
        $where=array();
        $where['status']=1;
        $where['is_delete']=0;
        $where['cate_id']=$cate_id;
        $count= M('article')->where($where)->count();
        $Page = new \Think\Page($count,$perpage);
        $list= M('article')->where($where)->Page($p,$perpage)->order($order)->select();
        foreach($list as $k=>$v){
            $list[$k]['introduce']=mb_substr(strip_tags(html_entity_decode($v['content'])),0,210,'utf-8');
            unset($list[$k]['content']);
        }
        $this->assign('list',$list);
        $this->assign('page',$Page->show());
        $this->display();
    }

    public function news(){
        //下面设置面包屑导航
        $new_id=I('get.id');
        $info=M('article')->alias('a')
            ->join('LEFT JOIN __ARTICLE_CATE__ c ON a.cate_id=c.id')
            ->where(array('a.id'=>$new_id,'a.status'=>1,'a.is_delete'=>0))
            ->field('a.id,a.cate_id,a.name,a.pic,a.user_id,a.content,a.hits,a.create_time,a.update_time,a.seo_title,a.seo_keys,a.seo_desc,c.name as cate_name')
            ->find();
        //下面设置搜索
        $seo_title=$info['seo_title']?$info['seo_title']:$info['name'].'-'.$info['cate_name'].'-'.$this->all_setting['site_name'].$this->all_config['seo_default_title'];
        $seo_keys=$info['seo_keys']?$info['seo_keys']:$info['name'].'-'.$info['cate_name'].'-'.$this->all_setting['site_name'].$this->all_config['seo_default_keys'];
        $seo_desc=$info['seo_desc']?$info['seo_desc']:$info['name'].'-'.$info['cate_name'].'-'.$this->all_setting['site_name'].$this->all_config['seo_default_desc'];
        $this->assign('seo_array',array('seo_title'=>$seo_title, 'seo_keys'=>$seo_keys, 'seo_desc'=>$seo_desc));
        $this->assign('mianbaoxie',array(
            array('name'=>'首页','url'=>'/'),
            array('name'=>$info['cate_name'],'url'=>U('Home/index/list1',array('id'=>$info['cate_id']))),
            array('name'=>$info['name'],'url'=>U('Home/index/news',array('id'=>$new_id)))
        ));
        $this->assign('info',$info);
        $this->display();
    }

    public function product(){
        $get=I('get.');
        $p = $get['p'] ? $get['p'] : 1 ;
        $order='id desc';
        $perpage=5;
        $this->assign('mianbaoxie',array(
            array('name'=>'首页','url'=>'/'),
            array('name'=>'产品中心','url'=>U('Home/index/product')),
        ));
        //下面构建seo
        $seo_title='产品市场'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_title'];
        $seo_keys='产品市场'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_keys'];
        $seo_desc='产品市场'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_desc'];
        $this->assign('seo_array',array('seo_title'=>$seo_title, 'seo_keys'=>$seo_keys, 'seo_desc'=>$seo_desc));
        //下面是构建条件
        $where=array();
        $where['is_delete']=0;
        $where['is_display']=1;
        $count= M('product')->where($where)->count();
        $Page = new \Think\Page($count,$perpage);
        $list= M('product')->where($where)->Page($p,$perpage)->order($order)->select();
        $this->assign('list',$list);
        $this->assign('page',$Page->show());
        $this->display();
    }

    public function one_product(){
        $new_id=I('get.id');
        $info=M('product')->where(array('id'=>$new_id,'is_display'=>1,'is_delete'=>0))->find();
        //下面设置seo
        $seo_title=$info['seo_title']?$info['seo_title']:$info['name'].'-产品市场'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_title'];
        $seo_keys=$info['seo_keys']?$info['seo_keys']:$info['name'].'-产品市场'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_keys'];
        $seo_desc=$info['seo_desc']?$info['seo_desc']:$info['name'].'-产品市场'.'-'.$this->all_setting['site_name'].$this->all_config['seo_default_desc'];
        $this->assign('seo_array',array('seo_title'=>$seo_title, 'seo_keys'=>$seo_keys, 'seo_desc'=>$seo_desc));
        $this->assign('mianbaoxie',array(
            array('name'=>'首页','url'=>'/'),
            array('name'=>'产品中心','url'=>U('Home/index/product')),
            array('name'=>$info['name'],'url'=>U('Home/index/one_product',array('id'=>$new_id))),
        ));
        $this->assign('info',$info);
        $this->display();
    }
}